// pages/servicepay/servicepay.js
var appUser = getApp();
const db = wx.cloud.database()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    info:{},
    date:"",
    address:"",
    id:'',
    range:[
    ],
    name:"请选择服务对象",
    oldid:0,
    openid:""
  },

  onChange(e){
    var index=e.detail.value
    console.log(this.data.range[index])
    this.setData({
      name: this.data.range[index].name,
      oldid:this.data.range[index].id,
    })
},
  bindTimeChange(e){
const date=e.detail.value
  this.setData({
    date:date
  })
  console.log(this.data.date)
  },
  toaddress(){
    wx.navigateTo({
      url: '/modules/settingModule/pages/address/list/index?select='+1,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
 var info = wx.getStorageSync('userinfo')
 console.log(info)
   this.setData({
    openid:info._openid
   })
        this.setData({
          id:options.id
        })
        this.getInfo(options.id)
        this.getName();
  },
  //根据id查询出对应的服务详情
  getInfo(id){
      wx.cloud.callFunction({
        name:"service",
        data:{
          type:"selectById",
          id:id
        }
      }).then(res=>{
          this.setData({
            info:res.result.data
          })
      })
  },
  getName(){

    db.collection("oldmanlist").where({
      _openid: appUser.globalData.userOpenid
    }).get({
      success:res=>{
      var list=res.data
      list.map(item=>{
        this.data.range.push({name:item.name,id:item._id})
      })
      this.setData({
        range: this.data.range      
      })
    }
  })
  },
  submit(){
    console.log(this.data.address)
  if(this.data.address.length<1 ||this.data.date.length<1){
      wx.showToast({
        title: '请输入完整',
        icon:"error"
      })
    return
 }
 wx.cloud.callFunction({
   name:"order",
   data:{
     type:"add",
     address:this.data.address,
    date:this.data.date,
    pid:this.data.id,
    iscm:0,
    oldid: this.data.oldid,
     bz:this.data.remarks,
     xdid:this.data.openid  
   }
 }).then(res=>{
    console.log(res)
 })

    wx.showToast({
      title: '提交成功',
    })
  setTimeout(()=>{
    wx.reLaunch({
      url: '../index/index'
    })
  },1000)
      
  }



 
})